export default class MeasureManager {
  constructor(viewer) {
    this._viewer = viewer
    this._clampMode = Cesium.ClampMode.Ground
    this._handler = null
    this._handlerDis = null
    this._handlerArea = null
    this._handlerHeight = null

    this._init()
  }

  _init() {
    this._handler = new Cesium.ScreenSpaceEventHandler(
      this._viewer.scene.canvas
    )

    // 初始化测量距离
    this._handlerDis = new Cesium.MeasureHandler(
      this._viewer,
      Cesium.MeasureMode.Distance,
      this._clampMode
    )
    // 注册测距功能事件
    this._handlerDis.measureEvt.addEventListener(
      (result) => {
        const dis = Number(result.distance)
        const distance =
          dis > 1000
            ? (dis / 1000).toFixed(2) + 'km'
            : dis.toFixed(2) + 'm'
        this._handlerDis.disLabel.text = '距离:' + distance
      }
    )
    this._handlerDis.activeEvt.addEventListener(
      this._measureActiveEvent
    )

    // 初始化测量面积
    this._handlerArea = new Cesium.MeasureHandler(
      this._viewer,
      Cesium.MeasureMode.Area,
      this._clampMode
    )
    this._handlerArea.measureEvt.addEventListener(
      (result) => {
        const mj = Number(result.area)
        const area =
          mj > 1000000
            ? (mj / 1000000).toFixed(2) + 'km²'
            : mj.toFixed(2) + '㎡'
        this._handlerArea.areaLabel.text = '面积:' + area
      }
    )
    this._handlerArea.activeEvt.addEventListener(
      this._measureActiveEvent
    )

    // 初始化测量高度
    this._handlerHeight = new Cesium.MeasureHandler(
      this._viewer,
      Cesium.MeasureMode.DVH
    )
    this._handlerHeight.measureEvt.addEventListener(
      (result) => {
        const distance =
          result.distance > 1000
            ? (result.distance / 1000).toFixed(2) + 'km'
            : result.distance + 'm'
        const vHeight =
          result.verticalHeight > 1000
            ? (result.verticalHeight / 1000).toFixed(2) +
              'km'
            : result.verticalHeight + 'm'
        const hDistance =
          result.horizontalDistance > 1000
            ? (result.horizontalDistance / 1000).toFixed(
                2
              ) + 'km'
            : result.horizontalDistance + 'm'
        this._handlerHeight.disLabel.text =
          '空间距离:' + distance
        this._handlerHeight.vLabel.text =
          '垂直高度:' + vHeight
        this._handlerHeight.hLabel.text =
          '水平距离:' + hDistance
      }
    )
    this._handlerHeight.activeEvt.addEventListener(
      this._measureActiveEvent
    )
  }

  _measureActiveEvent = (isActive) => {
    if (isActive === true) {
      this._viewer.enableCursorStyle = false
      this._viewer._element.style.cursor = ''
      this._viewer.scene.pickPointEnabled = false
    } else {
      this._viewer.enableCursorStyle = true
      this._viewer.scene.pickPointEnabled = false
    }
  }

  _deactivateAll() {
    this._handlerDis && this._handlerDis.deactivate()
    this._handlerArea && this._handlerArea.deactivate()
    this._handlerHeight && this._handlerHeight.deactivate()
  }

  _clearAll() {
    this._deactivateAll()
    this._handlerDis && this._handlerDis.clear()
    this._handlerArea && this._handlerArea.clear()
    this._handlerHeight && this._handlerHeight.clear()
  }

  length() {
    // this._deactivateAll()
    this._handlerDis && this._handlerDis.activate()
  }

  height() {
    // this._deactivateAll()
    this._handlerHeight && this._handlerHeight.activate()
  }

  area() {
    // this._deactivateAll()
    this._handlerArea && this._handlerArea.activate()
  }

  clear() {
    this._clearAll()
  }
}
